//
//  ZNDrawController.m
//  ZNDraw
//
//  Created by Sniper on 16/1/2.
//  Copyright © 2016年 Sniper. All rights reserved.
//

#import "ZNDrawController.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define ZNMARGIN_TOP 85
#define ZNLEFTPOSITION -220
#define ZNRIGHTPOSITION 250

@interface ZNDrawController ()

@property (nonatomic, assign)BOOL isDraging;

@end

@implementation ZNDrawController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initZNDrawView];
    
    [self addFrameObserver];
    
}

- (void)initZNDrawView {
    UIView *leftView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:leftView];
    leftView.backgroundColor = [UIColor greenColor];
    _leftView = leftView;
    
    UIView *rightView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:rightView];
    rightView.backgroundColor = [UIColor cyanColor];
    _rightView = rightView;
    
    UIView *containerView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:containerView];
    containerView.backgroundColor = [UIColor orangeColor];
    _containerView = containerView;
}

#pragma mark - KVO
-  (void)addFrameObserver {
    [_containerView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if (_containerView.frame.origin.x > 0) {
        // 向又滑
        _leftView.hidden = NO;
        _rightView.hidden = YES;
    } else if (_containerView.frame.origin.x < 0) {
        // 向左滑
        _leftView.hidden = YES;
        _rightView.hidden = NO;
    }
    
}

#pragma mark - UITouch
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    
    CGPoint currentPoint = [touch locationInView:self.view];
    CGPoint prePoint = [touch previousLocationInView:self.view];
    CGFloat offsetX = currentPoint.x - prePoint.x;
    
    _containerView.frame = [self getContainerViewFrameWithOffset:offsetX];
    _isDraging = YES;
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    if (!_isDraging && _containerView.frame.origin.x != 0) {
        // 单击还原
        [UIView animateWithDuration:0.25 animations:^{
            _containerView.frame = self.view.bounds;
        }];
    }
    
    CGFloat position = 0;
    if (_containerView.frame.origin.x > SCREEN_WIDTH * 0.5) {
        // 向右滑动过半了
        position = ZNRIGHTPOSITION;
    } else if (CGRectGetMaxX(_containerView.frame) < SCREEN_WIDTH * 0.5) {
        // 向左滑动过半了
        position = ZNLEFTPOSITION;
    }
    
    [UIView animateWithDuration:0.25 animations:^{
        if (position != 0) {
            CGFloat offsetX = position - _containerView.frame.origin.x;
            _containerView.frame = [self getContainerViewFrameWithOffset:offsetX];
            
        } else {
            _containerView.frame = self.view.bounds;
        }
    }];
    _isDraging = NO;
}

#pragma mark - calculate containerview
- (CGRect)getContainerViewFrameWithOffset:(CGFloat)offsetX {
    CGFloat offsetY = offsetX * ZNMARGIN_TOP / SCREEN_WIDTH;
    CGFloat scale = (SCREEN_HEIGHT - 2 * offsetY) / SCREEN_HEIGHT;
    
    if (_containerView.frame.origin.x < 0) {
        // 向左滑
        scale = (SCREEN_HEIGHT + 2 * offsetY) / SCREEN_HEIGHT;
    }
    
    CGRect frame = _containerView.frame;
    frame.origin.x += offsetX;
    frame.size.height = frame.size.height * scale;
    frame.size.width = frame.size.width * scale;
    frame.origin.y = (SCREEN_HEIGHT - frame.size.height) * 0.5;
    
    return frame;
}

@end
